package org.nf.partner.dao.impl;

import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.ColumnHandler;
import org.nf.partner.dao.PartnerDAO;
import org.nf.partner.model.Partner;
import org.nf.product.model.Product;
import org.nf.product.util.ConnUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 合作商dao 接口实现
 */
public class PartnerDaoImpl implements PartnerDAO {
    /**
     * 添加合作商
     * @param partner
     * @return
     */
    @Override
    public int insertPartner(Partner partner) {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="INSERT into design_partnerinfo(partner_name,partner_website,partner_details) VALUES(?,?,?)";
        return executor.executeUpdate(sql,partner.getPartnerName(),partner.getPartnerWebsite(),partner.getPartnerDetails());
    }

    /**
     * 删除合作商
     * @param partnerId
     * @return
     */
    @Override
    public int deletePartner(int partnerId) {
        //实例化SqlExecutor对象 提供删除操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="delete from design_partnerinfo where partner_id=?";
        return executor.executeUpdate(sql,partnerId);
    }

    /**
     * 修改合作商
     * @param partner
     * @return
     */
    @Override
    public int updatePartner(Partner partner) {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="update design_partnerinfo set partner_name=?,partner_website=?,partner_details=? where partner_id=?";
        return executor.executeUpdate(sql,partner.getPartnerName(),partner.getPartnerWebsite(),partner.getPartnerDetails());
    }

    /**
     * 根据合作商名称查询
     * @param partnerName
     * @return
     */
    @Override
    public List<Partner> selectPartnerName(String partnerName) {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<Partner> handler = new BeanListHandler<>(Partner.class);
        String sql = "select partner_id,partner_name,partner_website,partner_details from design_partnerinfo where partner_name LIKE '%'?'%'";
        return executor.executeQuery(sql,handler,partnerName);
    }

    /**
     * 分页查询
     * @param partner
     * @param numberOf 从第几条开始查询
     * @param limit 取多少条
     * @return
     */
    @Override
    public List<Partner> listPartnerPage(Partner partner, int numberOf, int limit) {
        //实例化SqlExecutor对象 提供增删改查操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        //定义一个集合，存储动态sql中的占位符
        List<Object> params = new ArrayList<>();
        //用于sql拼接
        StringBuilder sql = new StringBuilder("select partner_id,partner_name,partner_website,partner_details");
        sql.append(" from design_partnerinfo where 1=1");

        if (partner !=null && partner.getPartnerName()!= null && !"".equals(partner.getPartnerName().trim())){
            sql.append(" and partner_name like '%'?'%'");
            params.add(partner.getPartnerName());
        }
        sql.append("limit ?,?");
        BeanListHandler<Partner> handler = new BeanListHandler<>(Partner.class);
        return executor.executeQuery(sql.toString(), handler, numberOf, limit);

    }

    /**
     * 查询全部合作商
     * @param partner
     * @return
     */
    @Override
    public List<Partner> listPartner(Partner partner) {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<Partner> handler = new BeanListHandler<>(Partner.class);
        String sql = "select partner_id,partner_name,partner_website,partner_details from design_partnerinfo";

        return executor.executeQuery(sql,handler);
    }

    /**
     * 计算总页数
     * @return
     */
    @Override
    public Long countPartner() {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select count(*) from design_partnerinfo";
        ColumnHandler<Long> handler = new ColumnHandler<>(1);
        return executor.executeQuery(sql, handler);
    }
}
